package tutorial.lite_mod.tag_4_goods;

import com.google.common.base.Joiner;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import public_util.model.Const4PublicDataBase;
import public_util.model.PublicDataAdminUser;
import public_util.model.PublicMiddleTable;
import public_util.model.ReturnData;
import system_api_4_trade.dao.ApiPublicMiddleTableMapper;
import system_api_4_trade.util.sql.SelectTool;
import system_api_4_trade.util.tool_4_handle.StringToolApi;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * https://note.youdao.com/ynoteshare/index.html?id=3c88200c51f951f6e18fa590b6e3f6c8&type=note&_time=1663293584446
 * 传递特殊参数, 可以演示的模板部分
 * 后面大部分的配置, 都是前端自己传过来比较稳妥
 * 每个传参对应一个相应的方法
 */
public class TemplateUtilApi {

    private static Logger logger = Logger.getLogger(TemplateUtilApi.class);


    /**
     * 工具零件方法
     * 组装方法: 主要是在before中
     * 这两个方法是:搜索的时候, 可以把ID搜索出来组装进去即可
     * public_page_4m_load_goods_list
     * public_page_4m_shop_goods_list_2_reg
     * 根据这个零件来拼装
     * 将相关的id进行组装一把
     *
     * @return
     */
    public static ReturnData pre_cmd_para_show_tag_goods(HttpServletRequest request, SqlSession session, HashMap<String, Object> params2db, ReturnData resultData)
    {
        ApiPublicMiddleTableMapper middleTableMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        String pre_cmd_para_1_ids = request.getParameter("pre_cmd_para_1_ids");
        String pre_cmd_para_2_ids = request.getParameter("pre_cmd_para_2_ids");
        pre_cmd_para_2_ids = StringToolApi.paraHttpFilter(pre_cmd_para_2_ids);
        pre_cmd_para_1_ids = StringToolApi.paraHttpFilter(pre_cmd_para_1_ids);
        if (pre_cmd_para_2_ids == null && pre_cmd_para_1_ids == null)
        {
            return null;
        }
        String id_list_goods = find_id_goods_4_tag(pre_cmd_para_1_ids,pre_cmd_para_2_ids, middleTableMapper);
        if (id_list_goods == null)
        {
            id_list_goods = "-1";//啥都没搜,也不能放过
        }
        params2db.put("public_goods_info.id_public_goods_info_in", id_list_goods);
        return null;
    }



    /**
     * 根据标签把ID一个个查出来
     * @param pre_cmd_para_1_ids
     * @param pre_cmd_para_2_ids
     * @param middleTableMapper
     * @return
     */
    private static String find_id_goods_4_tag(String pre_cmd_para_1_ids, String pre_cmd_para_2_ids, ApiPublicMiddleTableMapper middleTableMapper)
    {
        HashMap<String, Object> paraPrice = new HashMap<String, Object>();
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 32001);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_mid_table_remark_003+ "_in", pre_cmd_para_1_ids);
        paraPrice.put(Const4PublicDataBase.PARA_mid_table_remark_002+ "_in", pre_cmd_para_2_ids);
        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraPrice, "public_middle_table_lite_mod_tag_4_goods");
        List<PublicMiddleTable> listQianyuePrice = middleTableMapper.tableDataList(realPara);//查询是否具备值
        if (listQianyuePrice == null || listQianyuePrice.size() == 0)
        {
            return null;
        }
        //返回逗号隔开的ID
        List<Integer> listInteger = listQianyuePrice.stream().map(PublicMiddleTable::getId_public_goods_info).collect(Collectors.toList());
        String list_id_str = Joiner.on(",").join(listInteger);
        return list_id_str;
    }




}
